library(readr)
library(irr)
library(gmodels)  # For contingency tables

# Load the datasets
df_a <- read_csv("Form A.csv")
df_b <- read_csv("Form B.csv")

# Calculate weighted Cohen's kappa for Form A
kappa_a <- kappa2(df_a[, c("Rater 1", "Rater 2")], weight = "squared")
print(paste("Weighted Cohen's kappa for Form A:", round(kappa_a$value, 3)))

# Contingency table for Form A with labels
contingency_a <- as.data.frame.matrix(table(df_a$`Rater 1`, df_a$`Rater 2`))
colnames(contingency_a) <- paste("Rater 2:", colnames(contingency_a))
rownames(contingency_a) <- paste("Rater 1:", rownames(contingency_a))
print("Contingency Table for Form A:")
print(contingency_a)

# Calculate weighted Cohen's kappa for Form B
kappa_b <- kappa2(df_b[, c("Rater 1", "Rater 2")], weight = "squared")
print(paste("Weighted Cohen's kappa for Form B:", round(kappa_b$value, 3)))

# Contingency table for Form B with labels
contingency_b <- as.data.frame.matrix(table(df_b$`Rater 1`, df_b$`Rater 2`))
colnames(contingency_b) <- paste("Rater 2:", colnames(contingency_b))
rownames(contingency_b) <- paste("Rater 1:", rownames(contingency_b))
print("Contingency Table for Form B:")
print(contingency_b)